﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SubSpec;
using Xunit;
using SqliteORM.Tests.Model;

namespace SqliteORM.Tests
{
    public class TransactionTests
    {
        [Specification]
        public void TransactionRequiresExplicitCommit()
        {
            const int TestId = 20;

            "Given TableIdSting table with data".Context( () =>
            {                
                Context.Initialise();
                DbConnection.Initialise( Context.ConnectionString );
                TableIdString data = new TableIdString() { Id = TestId, Text = "Hello" };
                data.Save();
            });

            "When Deleting in a transaction without explicit commit".Do( () => 
            {
                using (DbTransaction transaction = DbTransaction.Open())
                {
                    var x = TableIdString.Read( TestId );
                    TableIdString.Delete( TestId );                    
                }
            } );

            "Then no data deleted".Observation( () =>
                {
                    Assert.NotNull( TableIdString.Read( TestId ) );
                } );
        }

        [Specification]
        public void TransactionWithExplicitCommit()
        {
            const int TestId = 20;

            "Given TableIdSting table with data".Context( () =>
            {
                Context.Initialise();
                DbConnection.Initialise( Context.ConnectionString );
                TableIdString data = new TableIdString() { Id = TestId, Text = "Hello" };
                data.Save();
            } );

            "When Deleting in a transaction with  explicit commit".Do( () =>
            {
                using (DbTransaction transaction = DbTransaction.Open())
                {
                    TableIdString.Delete( TestId );
                    transaction.Commit();
                }
            } );

            "Then data was deleted".Observation( () =>
            {
                Assert.Null( TableIdString.Read( TestId ) );
            } );
        }

    }
}
